package com.ta.gulimall.cart.controller;

import com.ta.gulimall.cart.service.CartService;
import com.ta.gulimall.cart.view.vo.Cart;
import com.ta.gulimall.cart.view.vo.CartItem;
import com.ta.gulimall.common.utils.system.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {
    @Autowired
    CartService cartService;

    /**
     * 结算选中的购物车商品
     * @return
     */
    @GetMapping("/checkCart")
    @ResponseBody
    public R checkCart(){
        List<CartItem> list = cartService.checkCart();
        return R.ok(list);
    }

    /**
     * 查询单个购物车商品
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/success.html")
    public String getCart(Long skuId, Model model){
        CartItem cartItem = cartService.getCartItem(skuId);
        model.addAttribute("item", cartItem);

        return "success";
    }

    /**
     * 用户或者临时用户的购物车商品
     * @return
     */
    @GetMapping("/cartList.html")
    public String cartList(Model model){
        Cart cart = cartService.cartList();
        model.addAttribute("cart", cart);

        return "cartList.html";
    }

    /**
     * 添加购物车
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/addCart")
    public String addCart(Long skuId, Integer num) throws ExecutionException, InterruptedException {
        cartService.addCart(skuId, num);

        return "redirect:http://cart.gulimall.com/success.html?skuId=" + skuId;
    }

    /**
     * 修改购物车数量
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping("/cartCount")
    public String cartCount(Long skuId, Integer num){
        cartService.cartCount(skuId, num);

        return "redirect:http://cart.gulimall.com/cartList.html";
    }

    /**
     * 修改选中状态
     * @param skuId
     * @param status
     * @return
     */
    @GetMapping("/cartSelected")
    public String cartSelected(Long skuId, Boolean status){
        cartService.cartSelected(skuId, status);

        return "redirect:http://cart.gulimall.com/cartList.html";
    }

    /**
     * 删除购物车商品
     * @param skuId
     * @return
     */
    @GetMapping("/cartDelete")
    public String cartDelete(Long skuId){
        cartService.cartDelete(skuId);

        return "redirect:http://cart.gulimall.com/cartList.html?";
    }

}
